package hikari.gui.panels.particle;

import com.gooddies.events.ValueChangedEvent;
import com.gooddies.swing.RowLayout;
import com.gooddies.texteditors.DefaultFloatTextField;
import hikari.gui.guicomponents.hLabel;
import hikari.math.CircleDrawer;
import hikari.persistence.file.FileSection;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import org.swig.jirr.IParticleEmitter;
import org.swig.jirr.IParticleSphereEmitter;
import org.swig.jirr.IParticleSystemSceneNode;
import org.swig.jirr.IVideoDriver;
import org.swig.jirr.vector3df;

/**
 * @author sad
 */
public class SphereEmitterEditorPanel extends EmitterEditorPanel {

    private IParticleSphereEmitter emmiter;
    private DefaultFloatTextField radius;
    private Vector3dEditor center;
    private final vector3df xCircle = vector3df.DIR_RIGHT;
    private final vector3df yCircle = vector3df.DIR_UP;
    private final vector3df zCircle = vector3df.DIR_FRONT;

    public SphereEmitterEditorPanel() {
        setTitle("Sphere Emitter");
        setLayout(new RowLayout().setStartYGap(20).setStartXGap(10).setEndYGap(10));
        add(new hLabel("Sphere Radius"));
        radius = new DefaultFloatTextField(0, 999999);
        radius.setPreferredSize(new Dimension(80, 20));

        radius.setValueChangedEventNumber(new ValueChangedEvent<Float>() {
            @Override
            public void valueChanged(Float value) {
                radiusChanged(value);
            }
        });
        add(radius, RowLayout.NewRow);

        add(new hLabel("Sphere Position"), RowLayout.NewRow);
        center = new Vector3dEditor(this);
        center.setValueChangedEvent(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                centerChanged();
            }
        });
    }

    private void radiusChanged(float newRadius) {
        emmiter.setRadius(newRadius);
    }

    private void centerChanged() {
        vector3df pos = center.getValue();
        emmiter.setCenter(pos);
    }

    @Override
    public void putFromEmmiter(IParticleEmitter particleEmitter) {
        this.emmiter = (IParticleSphereEmitter) particleEmitter;
        center.setValue(this.emmiter.getCenter());
        radius.setValue(this.emmiter.getRadius());
    }

    @Override
    public void putToEmmiter(IParticleEmitter emmiter) {
    }

    @Override
    public void renderEmitter(IVideoDriver driver) {
        driver.setMaterial(getMaterial());
        final float coeficient = 0.588235294f;//this coeficient is for radius of emmiter and radius in system
        vector3df tCenter = emmiter.getCenter();
        float tRadius = emmiter.getRadius() / coeficient;
        CircleDrawer drawer;
        drawer = new CircleDrawer(40, tCenter, xCircle, tRadius);
        drawer.drawCircle();

        drawer = new CircleDrawer(40, tCenter, yCircle, tRadius);
        drawer.drawCircle();

        drawer = new CircleDrawer(40, tCenter, zCircle, tRadius);
        drawer.drawCircle();
    }

    public static void writeData(FileSection parentSection, IParticleEmitter emitter) throws IOException {
        IParticleSphereEmitter spEmitter = (IParticleSphereEmitter) emitter;
        FileSection sect = parentSection.createNewSection("sphereEm");
        sect.writeVector("center", spEmitter.getCenter());
        sect.writeFloat("radius", spEmitter.getRadius());
        sect.close();
    }

    public static IParticleSphereEmitter loadData(IParticleSystemSceneNode partSystem, FileSection parentSection) throws IOException {
        IParticleSphereEmitter emitter = partSystem.createSphereEmitter(vector3df.NULLVECTOR, 1f);
        FileSection settings = parentSection.readSection(parentSection.getChunk("sphereEm"));
        vector3df center = settings.readVector(settings.getChunk("center"));
        float radius = settings.readFloat(settings.getChunk("radius"));

        emitter.setCenter(center);
        emitter.setRadius(radius);

        partSystem.setEmitter(emitter);
        settings.close();
        return emitter;
    }
}
